/**
 * 
 */
package controler;

import java.io.IOException;
import java.net.UnknownHostException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

/**
 * @author z0nk
 *
 */
public abstract class AProvider implements IProvider {

	/**
	 * 
	 */
	public String loginPage = null;
	protected CookieStore cookieStore = new BasicCookieStore();
	protected HttpContext localContext = new BasicHttpContext();
	protected HttpClient httpclient = new DefaultHttpClient();
	
	public AProvider() {
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see controler.IProvider#Buy(controler.Item)
	 */
	public Boolean Buy(Item p, Credentials c) {
		// TODO Auto-generated method stub
		
		return p.GetCurrentPrice() < p.getMaxPrice();
	}

	/* (non-Javadoc)
	 * @see controler.IProvider#GetPrice(controler.Item)
	 */
	@Override
	public Float GetPrice(Item p) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see controler.IProvider#GetName(controler.Item)
	 */
	@Override
	public String GetName(Item p) throws ItemNotFound {
		return p.getName();
	}

	public String getPage(String url){
		HttpClient httpClient = new DefaultHttpClient();
		
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		HttpGet get = new HttpGet(url);
		
		
		String result = null;

		try {
			result = httpClient.execute(get, responseHandler);
		} catch (HttpResponseException e) {
			System.out.println(e.getMessage());
			return null;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			result = "The host is invalid: " + url;
		} catch (IOException e) {
			e.printStackTrace();
		}

		return result;

	}

	
	

}
